/*
 * SendSMS.h
 *
 *  Created on: 2010-1-14
 *      Author: zet
 */

#ifndef SENDSMS_H_
#define SENDSMS_H_

#include<stdio.h>
#include<iostream>

#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/timer.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/regex.hpp>
#include <boost/asio.hpp>


#include "EUCPCInterface.h"
using namespace MTNMsgApi_NP;

/////////////字符转换头文件///////////////
#include <iconv.h>
#define OUTLEN 1000

/////////////字符编码转换类 utf-8 <--> gb2312 -- begin ///////////

// 字符编码码转换操作类
class CodeConverter
{
private:
    iconv_t cd;
public:
    // 构造
    CodeConverter(const char *from_charset, const char *to_charset)
    {
        cd = iconv_open(to_charset, from_charset);
    }

    // 析构
    ~CodeConverter()
    {
        iconv_close(cd);
    }

    // 转换输出
    int convert(char *inbuf, int inlen, char *outbuf, int outlen)
    {
        char **pin = &inbuf;
        char **pout = &outbuf;

        memset(outbuf, 0, outlen);
        return iconv(cd, pin, (size_t *)&inlen, pout, (size_t *)&outlen);
    }
};
/////////////字符编码转换类 utf-8 <--> gb2312 -- end ///////////

/////////////////////////////////////////////

class SMServer {
public:

	static int registFlag;	//注册标志

 struct SMParam_tag {
	char *sn;		//序列号 19
	char *pwd;		//密码 6
	char *EntName;	//企业名称 60
	char *LinkMan;	//联系人名称 20
	char *LinkPhone;	//联系电话 20
	char *LinkMobile;	//联系手机 15
	char *LinkEmail;	//电子邮件 60
	char *Fax;		//联系传真 20
	char *companyAddress;	//公司地址 60
	char *Postcode;	//邮政编码 6
	char *balance;	//余额 10
	char *voucherCardNo;		//充值卡号号
	char *voucherCardPass;		//充值卡密码
	char *mobilString;		//手机号 最多1000个，用逗号隔开.如：13012345678,13112345678,13212345678
	char *messageString;		//短信内容 最多500个汉字或1000个字母。
	char *priority;	//优先级 5、4、3、2、1 高－－>低
	int  seqnum;	//短信ID
	//char *addi;		//扩展号 10
	char *sendtime;	//预定发送时间 格式：yyyymmddhhnnss(年月日时分秒)
	char *mobile;	//手机号
	//char *senderaddi; //发送者扩展号
	//char *recvaddi;		//接受者扩展号
	char *receiveTime;		//接收时间 格式:yyyymmddhhnnss(年月日时分秒)
	int  *flag;		//标志 1：表示有短信，0：表示无短信
	//char *errorcode;//状态错误码
	//char *reptype;	// 1 为发送失败状态报告
	char *phoneString;	//手机号码 （最多10个，用逗号隔开）
	char *key;		//KEY值 6 (设置key值，可以使一个序列号在多台机器上使用)
	char *versionNo;		//版本号 32
} ;

struct SMParam_tag SMParam;

	SMServer(void);
	~SMServer();
	void initSMServer(void);
	int mobilToString(std::vector<std::string> mobilVector);	//把所有要发的手机号整合成一串
	int messageToString(std::vector<std::string> messageVector);				//组织短信内容

	int sendSMS(std::vector<std::string> mobilVector, std::vector<std::string> messageVector); //发送短信
};	//end class SMServer

//int registFlag=-999;	//error!!!
SMServer::SMServer()
{
	initSMServer();
}

SMServer::~SMServer()
{
	printf("SMServer::~SMServer---begin\n");


		if(this->SMParam.sn !=NULL) {delete []this->SMParam.sn;this->SMParam.sn=NULL;}		//序列号 19
		if(this->SMParam.pwd !=NULL) {delete []this->SMParam.pwd;this->SMParam.pwd=NULL;}		//密码 6
		if(this->SMParam.EntName !=NULL) {delete []this->SMParam.EntName;this->SMParam.EntName=NULL;}	//企业名称 60
		if(this->SMParam.LinkMan !=NULL) {delete []this->SMParam.LinkMan;this->SMParam.LinkMan=NULL;}	//联系人名称 20
		if(this->SMParam.LinkPhone !=NULL) {delete []this->SMParam.LinkPhone;this->SMParam.LinkPhone=NULL;}	//联系电话 20
		if(this->SMParam.LinkMobile !=NULL) {delete []this->SMParam.LinkMobile;this->SMParam.LinkMobile=NULL;}	//联系手机 15
		if(this->SMParam.LinkEmail !=NULL) {delete []this->SMParam.LinkEmail;this->SMParam.LinkEmail=NULL;}	//电子邮件 60
		if(this->SMParam.Fax !=NULL) {delete []this->SMParam.Fax;this->SMParam.Fax=NULL;}		//联系传真 20
		if(this->SMParam.companyAddress !=NULL) {delete []this->SMParam.companyAddress;this->SMParam.companyAddress=NULL;}	//公司地址 60
		if(this->SMParam.Postcode !=NULL) {delete []this->SMParam.Postcode;this->SMParam.Postcode=NULL;}	//邮政编码 6
		if(this->SMParam.balance !=NULL) {delete []this->SMParam.balance;this->SMParam.balance=NULL;}	//余额 10
		if(this->SMParam.voucherCardNo !=NULL) {delete []this->SMParam.voucherCardNo;this->SMParam.voucherCardNo=NULL;}		//充值卡号号
		if(this->SMParam.voucherCardPass !=NULL) {delete []this->SMParam.voucherCardPass;this->SMParam.voucherCardPass=NULL;}		//充值卡密码
		if(this->SMParam.mobilString !=NULL) {delete []this->SMParam.mobilString;this->SMParam.mobilString=NULL;}		//手机号 最多1000个，用逗号隔开.如：13012345678,13112345678,13212345678
		if(this->SMParam.messageString !=NULL) {delete []this->SMParam.messageString;this->SMParam.messageString=NULL;}		//短信内容 最多500个汉字或1000个字母。
		if(this->SMParam.priority !=NULL) {delete []this->SMParam.priority;this->SMParam.priority=NULL;}	//优先级 5、4、3、2、1 高－－>低
			//短信ID
		//if(addi !=NULL) {delete []this->SMParam.;this->SMParam.=NULL;}		//扩展号 10
		if(this->SMParam.sendtime !=NULL) {delete []this->SMParam.sendtime;this->SMParam.sendtime=NULL;}	//预定发送时间 格式：yyyymmddhhnnss(年月日时分秒)
		if(this->SMParam.mobile !=NULL) {delete []this->SMParam.mobile;this->SMParam.mobile=NULL;}	//手机号
		//if(senderaddi !=NULL) {delete []this->SMParam.senderaddi;this->SMParam.senderaddi=NULL;} //发送者扩展号
		//if(recvaddi !=NULL) {delete []this->SMParam.recvaddi;this->SMParam.recvaddi=NULL;}		//接受者扩展号
		if(this->SMParam.receiveTime !=NULL) {delete []this->SMParam.receiveTime;this->SMParam.receiveTime=NULL;}		//接收时间 格式:yyyymmddhhnnss(年月日时分秒)
		if(this->SMParam.flag !=NULL) {delete []this->SMParam.flag;this->SMParam.flag=NULL;}		//标志 1：表示有短信，0：表示无短信
		//if(errorcode !=NULL) {delete []this->SMParam.errorcode;this->SMParam.errorcode=NULL;}//状态错误码
		//if(reptype !=NULL) {delete []this->SMParam.reptype;this->SMParam.reptype=NULL;}	// 1 为发送失败状态报告
		if(this->SMParam.phoneString !=NULL) {delete []this->SMParam.phoneString;this->SMParam.phoneString=NULL;}	//手机号码 （最多10个，用逗号隔开）
		if(this->SMParam.key !=NULL) {delete []this->SMParam.key; this->SMParam.key =NULL;}		//KEY值 6 (设置key值，可以使一个序列号在多台机器上使用)
		if(this->SMParam.versionNo !=NULL) {delete []this->SMParam.versionNo;this->SMParam.versionNo=NULL;}		//版本号 32
		printf("SMServer::~SMServer---end\n");
}

void SMServer::initSMServer(void)
{
	////以下是注册所需信息--begin /////////////////////

	SMParam.sn=(char *)malloc(20);
	char snno[]="3SDK-EMY-0130-KBZTK";		//序列号
	memcpy(SMParam.sn,snno,sizeof(snno));
	SMParam.sn[19]='\0';

	SMParam.pwd=(char *)malloc(7);
	char pwdno[]="569095";		//密码
	memcpy(SMParam.pwd,pwdno,sizeof(pwdno));
	SMParam.pwd[6]='\0';

	SMParam.EntName=(char *)malloc(62);
	char entNameno[]="北京顺福群聚科技有限公司";		//企业名称
	memcpy(SMParam.EntName,entNameno,sizeof(entNameno));
	SMParam.EntName[61]='\0';

	SMParam.LinkMan=(char *)malloc(22);
	char linkName[]="俞希群";		//联系人名称
	memcpy(SMParam.LinkMan,linkName,sizeof(linkName));
	SMParam.LinkMan[21]='\0';

	SMParam.LinkPhone=(char *)malloc(21);
	char linkPhone[]="62968986";	//联系人电话
	memcpy(SMParam.LinkPhone,linkPhone,sizeof(linkPhone));
	SMParam.LinkPhone[20]='\0';

	SMParam.LinkMobile=(char *)malloc(16);
	char linkMobil[]="13701075023";	//联系人手机
	memcpy(SMParam.LinkMobile,linkMobil,sizeof(linkMobil));
	SMParam.LinkMobile[15]='\0';

	SMParam.LinkEmail=(char *)malloc(61);
	char linkEmail[]="yuxiqun@263.net";		//联系人电子邮件
	memcpy(SMParam.LinkEmail,linkEmail,sizeof(linkEmail));
	SMParam.LinkEmail[60]='\0';

	SMParam.Fax=(char *)malloc(21);
	char linkFax[]="62968986";		//联系传真
	memcpy(SMParam.Fax,linkFax,sizeof(linkFax));
	SMParam.Fax[20]='\0';

	SMParam.companyAddress=(char *)malloc(62);
	char linkcompanyAddress[]="北京市海淀区上地信息路中黎科技园A座262室";		//公司地址
	memcpy(SMParam.companyAddress,linkcompanyAddress,sizeof(linkcompanyAddress));
	SMParam.companyAddress[61]='\0';

	SMParam.Postcode=(char *)malloc(7);
	char linkPostcode[]="100085";		//邮政编码
	memcpy(SMParam.Postcode,linkPostcode,sizeof(linkPostcode));
	SMParam.Postcode[6]='\0';

	////以上是注册所需信息--end /////////////////////

	SMParam.balance=(char *)malloc(11);
	char balanceno[]="0.00000000";		//余额
	memcpy(SMParam.balance,balanceno,sizeof(balanceno));
	SMParam.balance[10]='\0';

	SMParam.voucherCardNo=(char *)malloc(40);
	memset(SMParam.voucherCardNo,0,40);		//卡号
	SMParam.voucherCardNo[39]='\0';

	SMParam.voucherCardPass=(char *)malloc(40);
	memset(SMParam.voucherCardPass,0,40);		//卡密码
	SMParam.voucherCardPass[39]='\0';

	SMParam.mobilString=(char *)malloc(1000 * 15);	//手机号字符串
	memset(SMParam.mobilString,0,1000 * 15);
	SMParam.mobilString[999 * 15 + 14]='\0';

	SMParam.messageString=(char *)malloc(1001);	//短信内容
	memset(SMParam.messageString,0,1001);
	SMParam.messageString[1000]='\0';

	SMParam.priority=(char *)malloc(4);	//优先级
	memset(SMParam.priority,0,4);
	memcpy(SMParam.priority,"4",1);  //???
	SMParam.priority[1]='\0';		//???

	SMParam.sendtime=(char *)malloc(15);	//预定发送时间 格式：yyyymmddhhnnss(年月日时分秒)
	memset(SMParam.sendtime,0,15);
	SMParam.sendtime[14]='\0';

	SMParam.mobile=(char *)malloc(13);	//手机号
	memset(SMParam.mobile,0,13);
	SMParam.mobile[12]='\0';

	SMParam.receiveTime=(char *)malloc(15);	//接收时间 格式：yyyymmddhhnnss(年月日时分秒)
	memset(SMParam.receiveTime,0,15);
	SMParam.receiveTime[14]='\0';

	SMParam.flag=(int *)malloc(4);
	memset(SMParam.flag,0,4);

	SMParam.phoneString=(char *)malloc(10 * 15);	//手机号字符串
	memset(SMParam.phoneString,0,10 * 15);
	SMParam.phoneString[9 * 15 + 14]='\0';

	SMParam.key=(char *)malloc(7);	//key值
	memset(SMParam.key,0,7);
	SMParam.key[6]='\0';

	SMParam.versionNo=(char *)malloc(33);	//版本号
	memset(SMParam.versionNo,0,33);
	SMParam.versionNo[32]='\0';


	/////////////////错误码

}	//end function initSMServer()
/*
 * 功能：把参数里所有的手机号组成一个以逗号(,)隔开的合格的手机字符串
 * 参数说明：
 * mobilVector：存储所有的手机号
 * 返回值：
 * 0：没有手机号
 * 1：成功
 * －1：失败，手机号数大于1000个
 */
int SMServer::mobilToString(std::vector<std::string> mobilVector)
{
	std::size_t mobilNum=0;
	std::size_t i;
	std::string mobilStr="";
	//mobilStr.clear();
	mobilNum=mobilVector.size();
	if(mobilNum == 0) {		//没有手机号
		return 0;
	}
	else if(mobilNum == 1)	//若只有一个手机号
	{
		memcpy(this->SMParam.mobilString,mobilVector[0].c_str(),mobilVector[0].size());
	} else if(mobilNum > 1000) {	//手机号数>1000
		return -1;
	}
	else {	//手机号数在2~1000
		for( i=0; i < mobilNum - 1; i++) {
			mobilStr += mobilVector[i];
			mobilStr += ",";
		}
		mobilStr += mobilVector[i];
		//std::cout<<"mobil number="<<mobilNum<<",mobil string="<<mobilStr<<std::endl;
		//std::cout<<"mobil string size="<<mobilStr.size()<<std::endl;
		memcpy(SMParam.mobilString,mobilStr.c_str(),mobilStr.size());
	}

	return 1;
}	//end function mobileToString()

/*
 * 功能：组合短信内容
 * 参数说明：
 * 存储了要组成一串符合要求的短信
 * 返回值：
 * 0：短信字符数大于1000个
 * 1：成功
 */
int SMServer::messageToString(std::vector<std::string> mobilVector)
{
	std::string messageStr="";
	std::size_t i=0, messageSize=0;
	messageSize=mobilVector.size();
	if(messageSize > 1000) {
		return 0;
	}
	for(i=0;i< messageSize ;i++) {
		messageStr += mobilVector[i];
	}

	memcpy(SMParam.messageString,messageStr.c_str(),messageStr.size());
	return 1;
}
/*
 * 功能：发送短信
 * 参数说明：
 * mobilVector：保存所有手机号
 * messageVector：保存短信内容
 * 返回值：
 * 1 成功，0 失败
 */
int SMServer::sendSMS(std::vector<std::string> mobilVector, std::vector<std::string> messageVector)
{
	////////////////////////////  发送第二条短信   //////////////////////////////////

//		std::cout<<"接收短信的手机如下："<<std::endl;
//		for(std::size_t i=0;i<mobilVector.size();i++) {
//			std::cout<<"第 "<<i+1<<" 个手机号："<<mobilVector[i]<<std::endl;
//		}
//		std::cout<<"发送短信----begin"<<std::endl;
		////////////////////////////////
		int returnState=-999;		//返回状态码
						try {  // 调试SDK，短信服务.


						//SMServer sms;	//短信服务类 对象

						//std::vector<std::string> mobilVector;
						//mobilVector.clear();
						//mobilVector.push_back("13021274375");
		//		//		mobilVector.push_back("13021274376");
		//		//		mobilVector.push_back("13021274377");
		//		//		mobilVector.push_back("13021274378");
						this->mobilToString(mobilVector);		//ok 组织手机号
						//printf("mobil string=%s\n",this->SMParam.mobilString);


						//memcpy(sms.SMParam.mobilString,"13021274375",1000*15);	//接收短信的手机号字符串
						//memcpy(sms.SMParam.messageString,"北京顺福群聚科技有限公司![bjsfqj]",1000);	//短信内容
						//std::string mesgStr="第一条短信：发件人\"王欢\"于2009年12月09日通过\"申通\"快递公司的业务员\"张三\"(终端号：123456)发送了3件货物给您，";
				//		mesgStr += "预期3天后(2009年12月12日)送达。(短信来源：北京顺福群聚科技有限公司)";
				//		mesgStr += "希望您能长久地支持我们的工作，我们会给你最好的服务，快捷、有保证、安全等。";


						//std::string mesgStr="我们这里是北京顺福群聚科技有限公司，谢谢您的支持！[bjsfqjkjyxgs]";
//						std::vector<std::string> messageVector;
//						messageVector.clear();
//						messageVector.push_back(mesgStr);
						this->messageToString(messageVector);	//ok 组织短信内容
						//printf("message string=%s\n",SMParam.messageString);

				//////////////////////编码转换 begin//////////////////////

						char *in_utf8=this->SMParam.messageString;
						char messageInfo[OUTLEN];	//保存转换后的短信的内容
						// utf-8-->gb2312
						CodeConverter cc = CodeConverter("utf-8", "gb2312");
						cc.convert(in_utf8, strlen(in_utf8), messageInfo, OUTLEN);
						//std::cout << "utf-8-->gb2312 in=" << in_utf8 << ",messageInfo=" << messageInfo << std::endl;

				/////////////////////////编码转换 end///////////////////////////



//						returnState=MTNMsgApi_NP::UnRegister(this->SMParam.sn);
//						if(returnState == 1) {
//							printf("注销注册成功：returnState=%d\n",returnState);
//						}else {
//							printf("注销注册失败：returnState=%d\n",returnState);
//						}


						if(SMServer::registFlag != 1) {	//判断是否已注册
						//设置key值--being
						int keyFlag=-9;
						char key[9]="qinyikey";

						keyFlag=MTNMsgApi_NP::SetKey((char *)key);
						if(keyFlag == 1) {
							printf("设置key值成功。\n");
						}else {
							printf("设置key值失败keyFlag=%d\n",keyFlag);
							return keyFlag;
						}
						//设置key值--end


							printf("SMServer::registFlag=%d\n",SMServer::registFlag);
							printf("开始注册......\n");
							SMServer::registFlag=MTNMsgApi_NP::Register(this->SMParam.sn,this->SMParam.pwd,this->SMParam.EntName,
							this->SMParam.LinkMan,this->SMParam.LinkPhone,this->SMParam.LinkMobile,
							this->SMParam.LinkEmail,this->SMParam.Fax,this->SMParam.companyAddress,this->SMParam.Postcode);
							printf("注册返回值registFlag＝%d\n",SMServer::registFlag);

						if(SMServer::registFlag != 1) {	//注册失败，返回
							printf("注册失败.%d\n",SMServer::registFlag);
							return SMServer::registFlag;
						}else {
							printf("注册成功!\n");
						}
						}//判断是否已注册




//						MTNMsgApi_NP::GetSDKVersion(this->SMParam.versionNo);
//						printf("SDKVersion=%s\n",this->SMParam.versionNo);

						returnState=MTNMsgApi_NP::GetBalance(this->SMParam.sn,this->SMParam.balance);
						printf("returnState=%d,发送短信之前余额：%s\n",returnState,this->SMParam.balance);
						//returnState=MTNMsgApi_NP::GetPrice(this->SMParam.sn,this->SMParam.balance);
						//printf("returnState=%d,每条短信的费用=%s\n",returnState,this->SMParam.balance);
						//printf("message string=%s\n",this->SMParam.messageString);
						//std::cout<<"发送短信 前 时间："<<StringOperator::getCurrentTime()<<std::endl;
						returnState=MTNMsgApi_NP::SendSMS(this->SMParam.sn,this->SMParam.mobilString,messageInfo,this->SMParam.priority);
						//std::cout<<"发送短信 后 时间："<<StringOperator::getCurrentTime()<<std::endl;
//						if(in_utf8 != NULL) {
//							delete in_utf8;
//							in_utf8=NULL;
//						}

						if(returnState != 1)
						{
							printf("发送短信失败！returnState=%d\n",returnState);

						} else {
							printf("发送短信成功， returnState=%d\n",returnState);
						}

						returnState=MTNMsgApi_NP::GetBalance(this->SMParam.sn,this->SMParam.balance);
						printf("returnState=%d,发送短信之后余额：%s\n",returnState,this->SMParam.balance);

						} catch(std::exception &e) {
							std::cout<<"发送短信时出错!"<<std::endl;

						}	//调试sdk---end

		//std::cout<<"发送短信----end"<<std::endl;
		return returnState;

}	//SMServer::sendSMS

//////////////验证---begin

//class match

//////////////验证---end

#endif /* SENDSMS_H_ */
